package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CurriculumVO;
import edu.zqu.instructionalMatters.project.entity.Curriculum;
import edu.zqu.instructionalMatters.project.mapper.CurriculumMapper;
import edu.zqu.instructionalMatters.project.util.CurriculumUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表")
public class CurriculumController {
    @Autowired
    private CurriculumMapper curriculumMapper;
    @Autowired
    private CurriculumUtil curriculumUtil;

    @ApiOperation(value = "获取课表信息列表")
    @RequestMapping(value = "/curriculums", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = false),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = false),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = false),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = false),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = false),
            @ApiImplicitParam(name = "calendarDate", value = "校历日期", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId, BigInteger semesterId, BigInteger calendarId,
                             BigInteger timeTableId, BigInteger instructionClassId,
                             BigInteger courseId, BigInteger fieldId, BigInteger teacherId, String calendarDate) {
        CurriculumVO curriculumVO1 = (CurriculumVO) new CurriculumVO()
                .setCollegeId(collegeId).setSemesterId(semesterId).setCalendarId(calendarId).setTimeTableId(timeTableId)
                .setInstructionClassId(instructionClassId).setCourseId(courseId).setFieldId(fieldId).setTeacherId(teacherId);   //合成 CurriculumVO
        curriculumVO1.setCalendarDate(calendarDate);                                                                            //设置 calendarDate
        System.out.println(curriculumVO1);
        List<Curriculum> list1 = curriculumMapper.findAll(curriculumVO1);                                                       //获取 List<Curriculum>
        List<CurriculumVO> list = new ArrayList<>();                                                                            //初始化 List<CurriculumVO>
        for (Curriculum curriculum : list1) {                                                                                    //循环遍历赋值
            CurriculumVO curriculumVO = curriculumUtil.curriculumUtil(curriculum);                                               //调用工具类完成赋值
            list.add(curriculumVO);                                                                                              //加入到 list
        }
        return ZQUResult.success(list);                                                                                         //返回 list
    }

    @ApiOperation(value = "获取课表信息", response = CurriculumVO.class)
    @RequestMapping(value = "/curriculum/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课表id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) throws Exception {
        Curriculum curriculum = curriculumMapper.selectById(id);                                                             //查出对象
        CurriculumVO curriculumVO = curriculumUtil.curriculumUtil(curriculum);                                               //调用工具类完成赋值
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumVO);                                            //成功返回数据对象
    }

    @ApiOperation(value = "上传excel，批量插入")
    @RequestMapping(value = "/excelToCurriculum", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult uploadExcel(@RequestParam("excelFile") MultipartFile excelFile,
                                 BigInteger collegeId,
                                 BigInteger byWho) {
        return ZQUResult.success();
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/curriculum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId, BigInteger semesterId, BigInteger calendarId, BigInteger schoolId, BigInteger timeTableId,
                         BigInteger instructionClassId, BigInteger courseId, BigInteger fieldId, BigInteger teacherId, BigInteger byWho) {
        Curriculum curriculum = new Curriculum().setCollegeId(collegeId);                     //初始化数据模型、设置学校id
        curriculum.setSemesterId(semesterId).setCalendarId(calendarId);                       //设置学期id、校历id
        curriculum.setSchoolId(schoolId).setTimeTableId(timeTableId);                         //设置校区id、作息表id
        curriculum.setInstructionClassId(instructionClassId).setCourseId(courseId);           //设置授课班级id、课程id
        curriculum.setFieldId(fieldId).setTeacherId(teacherId).setByWho(byWho);               //设置授课场地id、教师id、操作者
        curriculumMapper.insert(curriculum);                                                  //执行插入
        return ZQUResult.success();                                                           //操作成功返回
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/curriculum/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "semesterId", value = "学期id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "calendarId", value = "校历id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeTableId", value = "作息id", paramType = "query", required = true),
            @ApiImplicitParam(name = "instructionClassId", value = "授课班级id", paramType = "query", required = true),
            @ApiImplicitParam(name = "courseId", value = "课程id", paramType = "query", required = true),
            @ApiImplicitParam(name = "fieldId", value = "场地id", paramType = "query", required = true),
            @ApiImplicitParam(name = "teacherId", value = "教师id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId, @PathVariable BigInteger id, BigInteger semesterId, BigInteger calendarId,
                            BigInteger schoolId, BigInteger timeTableId, BigInteger instructionClassId,
                            BigInteger courseId, BigInteger fieldId, BigInteger teacherId, BigInteger byWho) {
        Curriculum curriculum = curriculumMapper.selectById(id);                   //先查出旧记录
        curriculum.setByWho(byWho);                                                //设置操作者
        curriculumMapper.updateById(curriculum);                                   //更新操作者
        curriculumMapper.deleteById(id);                                           //逻辑删除
        curriculum = new Curriculum().setCollegeId(collegeId);                     //初始化数据模型、设置学校id
        curriculum.setSemesterId(semesterId).setCalendarId(calendarId);            //设置学期id、校历id
        curriculum.setSchoolId(schoolId).setTimeTableId(timeTableId);              //设置校区id、作息表id
        curriculum.setInstructionClassId(instructionClassId).setCourseId(courseId);//设置授课班级id、课程id
        curriculum.setFieldId(fieldId).setTeacherId(teacherId);                    //设置授课场地id、教师id
        curriculum.setByWho(byWho).setLastVersion(id);                             //设置操作者、上一版本
        curriculumMapper.insert(curriculum);                                       //执行插入新纪录
        return ZQUResult.success();                                                //操作成功返回
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/curriculum/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id, BigInteger byWho) {
        Curriculum curriculum = curriculumMapper.selectById(id);//先查出旧记录
        curriculum.setByWho(byWho);                             //设置操作者
        curriculumMapper.updateById(curriculum);                //更新操作者
        curriculumMapper.deleteById(id);                        //逻辑删除
        return ZQUResult.success();                             //操作成功返回
    }
}

